/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sesion;

import Conexion.cConexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Emanuel
 */
public class Sesion {
    
    public static ArrayList colsutla(String sql){
        ArrayList regs = new ArrayList();
        
        try{
            cConexion db = new cConexion();
            Connection cn = db.conectar_ds();
            
            if(cn == null){
                regs = null;
            }else {
                Statement st = cn.createStatement();
                ResultSet rs = st.executeQuery(sql);
                ResultSetMetaData rm= rs.getMetaData();
                
                int numCols = rm.getColumnCount();
                
                String[] titCols= new String[numCols];
                for(int i=0; i<numCols; ++i)
                    titCols[i] = rm.getColumnName(i+1);
                
                regs.add(titCols);
                
                
                while(rs.next()){
                    String[] reg = new String[numCols];
                    
                    for (int i=0; i<numCols; i++){
                        reg[i] = rs.getString(i +1);
                    }
                    regs.add(reg);
                }
                rs.close();
                st.close();
                cn.close();
            }
        }catch(SQLException e){
            regs=null;
        }catch(Exception e){
            regs=null;
        }
        return regs;
    }
    
    public static String[] getFila(String sql){
        
        ArrayList vector = colsutla(sql);
        String[] fila = null;
        
        if(vector != null)
            if(vector.size()>1)
                fila = (String[]) vector.get(1);
        
        return fila;
    }
    
}
